Public Class op_filter_PCsoft

Dim hComm As Integer 'COMポート用ハンドル
Dim stDCB As DCB 'DCBインスタンス
Dim timeOut As COMMTIMEOUTS 'タイムアウト用インスタンス
Dim wDATA As String 'COM送信バッファ
Dim rDATA(100) As Byte 'COM受信バッファ
Dim dLen, wLen, rLen As Int32 'COM用パラメータ
Dim bRet As Boolean '関数戻り値
Dim CommName As String 'COMポート番号

Dim Flag As Integer

Dim TB As String
Dim TBox(3000) As Integer

Dim Index, i As Integer
Dim N, M As Integer
Dim Data0(3000) As Integer

Private Sub TimeOutSet() 'COM用タイムアウト関数
timeOut.ReadIntervalTimeout = 20
timeOut.ReadTotalTimeoutConstant = 50
timeOut.ReadTotalTimeoutMultiplier = 10
timeOut.WriteTotalTimeoutMultiplier = 10
timeOut.WriteTotalTimeoutConstant = 10
bRet = SetCommTimeouts(hComm, timeOut)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox2.Text = ""
Timer1.Enabled = False
Index = 0


End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

CommName = TextBox1.Text 'COMポート番号取得
bRet = CloseHandle(hComm)

hComm = CreateFile(CommName, &HC0000000, 0, IntPtr.Zero, &H3, &H80, IntPtr.Zero)
If hComm <> -1 Then

stDCB.BaudRate = 115200 'DCB構造体データ設定
stDCB.fBitFields = &H1
stDCB.ByteSize = 8
stDCB.Parity = 0
stDCB.StopBits = 0
bRet = SetCommState(hComm, stDCB)
TimeOutSet()

If bRet = True Then '接続確認 コマンド「0」の応答「OK」をチェック
Application.DoEvents()
wDATA = Chr(&H30) 'コマンド0
dLen = 1

bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero)
bRet = ReadFile(hComm, rDATA, 30, rLen, IntPtr.Zero)
If bRet = True And rLen = 3 Then

If rDATA(0) = &H4F And rDATA(1) = &H4B Then ' OKが返信
TextBox2.Text = "Connect" '正常接続完了

Flag = 0
Else
TextBox2.Text = "False" '接続異常
End If
Else
TextBox2.Text = "NoAns" '応答なし
End If
Else
TextBox2.Text = "Fault" '接続不可
End If
Else
TextBox2.Text = "NoExist" 'デバイス見つからず
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Interval = 60000
Timer1.Enabled = True

wDATA = Chr(&H31) 'コマンド0x31送信
dLen = Len(wDATA)
bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero) 'コマンド送信後

End Sub


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Dim N, M As Integer
Dim g As Graphics = PictureBox1.CreateGraphics
Dim p1 As New Pen(Color.LightGray)
p1.Width = 5
Dim p2 As New Pen(Color.SkyBlue)
p2.Width = 5
Dim p3 As New Pen(Color.LightGreen)
p3.Width = 5
Dim p4 As New Pen(Color.Yellow)
p4.Width = 5
Dim p5 As New Pen(Color.Orange)
p5.Width = 5
Dim p6 As New Pen(Color.HotPink)
p6.Width = 5
Dim p7 As New Pen(Color.Red)
p7.Width = 5

Index = Index + 1

wDATA = Chr(&H31) 'コマンド0x31送信
dLen = Len(wDATA)

bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero) 'コマンド送信
bRet = ReadFile(hComm, rDATA, 64, rLen, IntPtr.Zero) 'データ受信(前回分)
If bRet = True Then

TB = ""
TextBox3.Text = ""


For M = 0 To 19 Step 1
For N = 3 * M To 3 * M + 1 Step 1
TB = TB & Chr(rDATA(N))

Next
Data0(Index) = Val(TB & Chr(0))
TextBox3.Text = TextBox3.Text & TB & Chr(&H20)

TBox((Index - 1) * 20 + M) = Data0(Index)

g.DrawLine(p1, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(Index) > 0 Then
g.DrawLine(p2, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(Index) > 2 Then
g.DrawLine(p3, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(Index) > 6 Then
g.DrawLine(p4, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(Index) > 12 Then
g.DrawLine(p5, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(Index) > 22 Then
g.DrawLine(p6, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(Index) > 40 Then
g.DrawLine(p7, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
End If
End If
End If
End If
End If
End If

TB = ""

Next
End If


End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Timer1.Enabled = False 'タイマー一時停止

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Me.Refresh()
Index = 0
TextBox3.Text = ""


End Sub


Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

Dim myFile As String = ""
Dim i As Integer
Dim tw As System.IO.TextWriter

SaveFileDialog1.Filter = "csvファイル(*.csv)|*.csv"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
myFile = SaveFileDialog1.FileName
End If
If myFile <> "" Then

Try
tw = My.Computer.FileSystem.OpenTextFileWriter(myFile, False, System.Text.Encoding.Default)
For i = 0 To (Index - 1) * 20 + 19
tw.WriteLine(Str(TBox(i)))
Next

tw.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

End If
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

Dim myFile As String = ""
Dim tr As System.IO.TextReader
Dim Item, i, j, k, M As Integer
Dim ss As String

Dim g As Graphics = PictureBox1.CreateGraphics
Dim p1 As New Pen(Color.LightGray)
p1.Width = 5
Dim p2 As New Pen(Color.SkyBlue)
p2.Width = 5
Dim p3 As New Pen(Color.LightGreen)
p3.Width = 5
Dim p4 As New Pen(Color.Yellow)
p4.Width = 5
Dim p5 As New Pen(Color.Orange)
p5.Width = 5
Dim p6 As New Pen(Color.HotPink)
p6.Width = 5
Dim p7 As New Pen(Color.Red)
p7.Width = 5

Index = 0
TextBox3.Text = ""

OpenFileDialog1.Filter = "csvファイル(*.csv)|*.csv"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
myFile = OpenFileDialog1.FileName

Try
tr = My.Computer.FileSystem.OpenTextFileReader(myFile, System.Text.Encoding.Default)
Item = 1
Do
ss = tr.ReadLine()
Data0(Item) = Val(ss)
Item = Item + 1
Loop While ss IsNot Nothing

tr.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try


For i = 1 To Item / 20
Index = Index + 1
M = 0
TextBox3.Text = "Time = " & Str(Index) & " min"

For j = 1 To 20
k = (i - 1) * 20 + j

g.DrawLine(p1, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(k) > 1 Then
g.DrawLine(p2, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(k) > 2 Then
g.DrawLine(p3, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(k) > 6 Then
g.DrawLine(p4, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(k) > 12 Then
g.DrawLine(p5, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(k) > 22 Then
g.DrawLine(p6, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
If Data0(k) > 40 Then
g.DrawLine(p7, 5 * Index, 200 - 10 * M, 5 * Index, 190 - 10 * M)
End If
End If
End If
End If
End If
End If
M = M + 1
Next
Next
End If
End Sub


End Class




  (Module1)


Module Module1

Public Structure DCB
'DCB構造体宣言
Public DCBlength As Int32
Public BaudRate As Int32
Public fBitFields As Int32
Public wReserved As Int16
Public XonLim As Int16
Public XoffLim As Int16
Public ByteSize As Byte
Public Parity As Byte
Public StopBits As Byte
Public XonChar As Byte
Public XoffChar As Byte
Public ErrorChar As Byte
Public EofChar As Byte
Public EvtChar As Byte
End Structure

Public Structure COMMTIMEOUTS
'タイムアウト用構造体宣言
Public ReadIntervalTimeout As Int32
Public ReadTotalTimeoutMultiplier As Int32
Public ReadTotalTimeoutConstant As Int32
Public WriteTotalTimeoutMultiplier As Int32
Public WriteTotalTimeoutConstant As Int32
End Structure

'COMポート用関数宣言
Public Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreatonDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, ByVal hTemplateFile As IntPtr) As Integer
Public Declare Function CloseHandle Lib "kernel32.dll" (ByVal hComm As Integer) As Boolean
Public Declare Function SetCommState Lib "kernel32.dll" (ByVal hComm As Integer, ByRef lpDCB As DCB) As Boolean
Public Declare Function WriteFile Lib "kernel32.dll" (ByVal hComm As Integer, ByVal lpBuffer As String, ByVal nNumberOfBytesToWrite As Int32, ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As Boolean
Public Declare Function ReadFile Lib "kernel32.dll" (ByVal hComm As Integer, ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean
Public Declare Function SetCommTimeouts Lib "kernel32.dll" (ByVal hComm As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Public Declare Function GetCommState Lib "kernel32.dll" (ByVal hComm As Integer, ByRef lpDCB As DCB) As Boolean



End Module